/* 页面定时器 */
setInterval("receiverMq()", 5000); /* 每过一秒执行一次displayTime */
setInterval("queryOrder()", 10000); /* 每过7秒执行一次打单 查询 */
setInterval("getNavPrintResult()", 10000); /* 每过1秒执行一次打单 */

// 调用后台接口查询订单消息
var receiverMq = function() {
	if (!shopIsOpen()) {
		return;
	}
	var url = ctx + "/admin/order/mq.do";
	$
			.ajax({
				type : "post",
				dataType : "json",
				url : url,
				async : false,
				success : function(data) {
					if (data.code == 0) {
						var msg = data.data;
						// 播放语音
						audio_play(msg);
						// 自动接单
						nativeWriteLog("receiverMq:" + JSON.stringify(msg));
						if (msg.type == "order_created") {// 新订单
							receiverOrder(msg.orderId);
						} else if (msg.type == "order_accepted") {// 接单消息，进行打单
							// console.info('msg', msg)
							if (msg.book == true) {// 预订单
								showBadge("admin/order/list.do?status=valid&type=all&book=1");
							}
						} else if (msg.type == "order_refund"||msg.type == "order_canceled") {
							showBadge("admin/order/list.do?status=apply_cancel,refunding&type=all");
						} else if (msg.type == 'deliver_exception') {
							showBadge("admin/deliver/list.do?status=-1");
						}
					} else if (data.code == -2) {
						sessionOut();
					} else {
						console.info("网络不稳定,运行失败");
						nativeWriteLog("receiverMq:网络不稳定,运行失败");
					}
				},
				error : function() {
					console.info("网络不稳定,运行失败");
					nativeWriteLog("receiverMq:网络不稳定,运行失败");
				}
			});
}

// 查询打单数据
var queryOrder = function() {
	if (!shopIsOpen()) {
		return;
	}
	var url = ctx + "/admin/order/queryOrder.do";
	$.ajax({
		type : "post",
		dataType : "json",
		url : url,
		async : false,
		success : function(data) {
			if (data.code == 0) {
				var list = data.data;
				var str = JSON.stringify(list);
				nativeWriteLog("queryOrder:" + str);
				var res = top.nativePrint(str);
				nativeWriteLog("nativePrint:---" + res);
				if (res) {
					updateOrderProcess(list, 1);
				}

			} else {
				console.info("网络不稳定,运行失败");
				nativeWriteLog("queryOrder:网络不稳定,运行失败");
			}
		},
		error : function() {
			console.info("网络不稳定,运行失败");
		}
	});
}
// 调用后台服务接单
var receiverOrder = function(orderId) {
	var url = ctx + "/admin/order/receiver.do";
	var params = {
		"orderId" : orderId,
	};
	$.ajax({
		type : "post",
		dataType : "json",
		data : params,
		url : url,
		async : false,
		success : function(data) {
			if (data.code == 0) {
				nativeWriteLog("receiverOrder:接单成功");
			} else {
				console.info("网络不稳定,运行失败");
				nativeWriteLog("receiverOrder:网络不稳定,运行失败");
			}
		},
		error : function() {
			console.info("网络不稳定,运行失败");
			nativeWriteLog("receiverOrder:网络不稳定,运行失败");
		}
	});
}
// 更新打印状态
var updateOrderProcess = function(list, state) {
	var array = new Array();
	for ( var i in list) {
		array.push(list[i].id);
	}
	if (array.length > 0) {
		var url = ctx + "/admin/order/updateProcess.do";
		$.ajax({
			type : "post",
			dataType : "json",
			data : {
				ids : array.join(),
				state : state
			},
			url : url,
			async : false,
			success : function(data) {
				if (data.code == 0) {
					console.info("[updateOrderProcess]success");
					nativeWriteLog("[updateOrderProcess]success");
				} else {
					console.info("[updateOrderProcess]error");
					nativeWriteLog("[updateOrderProcess]success");
				}
			},
			error : function() {
				console.info("[updateOrderProcess]网络不稳定,运行失败");
				nativeWriteLog("[updateOrderProcess]网络不稳定,运行失败");
			}
		});
	}
}
// 查询打印结果
var getNavPrintResult = function() {
	if (!shopIsOpen()) {
		return;
	}
	var content = nativeGetPrintResult();
	if (content != null && content != "null") {
		var data = JSON.parse(content);
		if (data.state == 3) {// 打印失败，通知
			var msg = {
				count : 1,
				src : "print_diconnect_ringtone.mp3"
			}
			audio_play(msg);
		} else {
			var array = new Array();
			var o = {
				"id" : data.orderId
			}
			array.push(o);
			updateOrderProcess(array, 2);
		}
	}
}
// 判断店铺是否开启自动接单
var shopIsOpen = function() {
//	var shopInfo = sessionStorage.getItem("shopInfo");
//	var shops = JSON.parse(shopInfo);
//	if (shops != null) {
//		for ( var i in shops) {
//			if (shops[i].orderAuto == 1) {
//				return true;
//			}
//		}
//	}
	return true;
}
// 
var showBadge = function(url) {
	$(".sidebar a.sub").each(function(i, v) {
		var href = $(this).attr("href");
		if (href.indexOf(url) >= 0) {
			var count = $(this).find("span.badge").text();
			$(this).find("span.badge").text(parseInt(count) + 1);
			$(this).find("span.badge").show();
		}
	});
}
